//引入样式
require('../../assets/css/normalize.css')
require("../../assets/css/reset.css")
require('../../assets/fonts/iconfont.css')
require('./register.less');

//引入模板
const axios = require('axios');
const Captcha = require('captcha-mini');
const dom = require('../../utils/dom.js');

document.addEventListener("DOMContentLoaded", function () {
    //创建验证码
    let captcha1 = new Captcha();
    let captchaInit = '';
    captcha1.draw(document.querySelector('#captcha1'), r => {
        captchaInit = r.toLowerCase();
    });
    // 表单验证
    dom.get("#reg-btn").addEventListener("click", function () {
        const reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
        let tel = dom.get('#tel').value;
        let error = dom.get("#error");
        if (reg.test(tel)) {
            error.textContent = '';
        } else {
            error.textContent = '请输入正确的手机号';
            return;
        }
        //验证码
        if (captchaInit === dom.get("#code").value.toLowerCase()) {
            error.textContent = '';
        } else {
            error.textContent = '验证码错误';
            return
        };
        //密码验证
        let pwd = dom.get('#pwd').value;
        if (pwd.length >= 6) {
            error.textContent = '';
        } else {
            error.textContent = '密码格式错误';
            return
        };
        //再次验证
        let pwd2 = dom.get('#confirm-pwd').value;
        if (pwd === pwd2) {
            error.textContent = '';
        } else {
            error.textContent = '两次密码输入不一致';
            return
        };
        axios.post('http://fitness.h5.itsource.cn:3701/api/user/register', {
            account: tel,
            password: pwd
        }).then(res => {
            console.log(res);
            if (res.data.errno === 0) {
                location.href = 'login.html';
                alert('恭喜你，注册成功！')
            } else {
                alert(res.data.message)
            }
        })
    })
})